/*
* 用到myj.js里一个方法
* 调用方法  $(obj).countryscreen(option,fn)
* option {callback:function(countrys){}}
* countrys = US,AU,CH
*
*
*/
(function($,win,doc,undefined){
    //format模板
    String.prototype.strFormat = function(args) {
        if (arguments.length>0){
            var result = this,
                reg = null;
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    reg = new RegExp ("(#{"+key+"})","g");
                    result = result.replace(reg, args[key]);
                }
            }else {
                for (var i = 0; i < arguments.length; i++) {
                    if(arguments[i]==undefined){
                        return "";
                    }else{
                        reg = new RegExp ("(#{["+i+"]})","g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
            return result;
        }else{
            return this;
        }
    };
    var unescapeHtml = (function() {
        var escapeMap = {
            '&amp;': '&',
            '&lt;': '<',
            '&gt;': '>',
            '&quot;': '"',
            '&apos;': '\'',
            '&#x60;': '`'
        };

        var source = '(?:' + getObjectKeys(escapeMap).join('|') + ')',
            testRegexp = RegExp(source),
            replaceRegexp = RegExp(source, 'g');

        var escaper = function(match) {
            return escapeMap[match];
        };

        return function(string) {
            string = string == null ? '' : '' + string;
            return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
        };
    })();
	$.fn.extend({
		countryscreen : function(option,fun){
            //newObj合并后的option,    allcountryData所有国家的json对象，   commoneData常用国家json对象，  allCountryQueryObj全部国家查询对象以code为属性名
			var newObj,allCountryData,commoneData,allCountryQueryObj={},
                htmlObj = $.fn.defaultsHtml,
			    _this = this;
            _this.empty();
			if(typeof option == 'object'){
                newObj = $.extend($.fn.defaults, option);
			}else{
                newObj = $.fn.defaults;
			}
            //生成整个组件html
            var outHtml = $(htmlObj.outHtml), //外层
                $oneListArr = [];
            outHtml.append(htmlObj.screenOptionModal + htmlObj.screenShowModal);//收起的两个状态DIV
            $.each(newObj.commonCountry,function(i, j){//初始单选项
                var oneList = $(htmlObj.aSel),
                    oneData = j;
                oneList.attr({
                    'data-value': oneData.code,
                    'uid': 'countryTag'
                }).html(oneData.name);
                $oneListArr.push(oneList);
            });
            //默认显示a状态
            outHtml.find('.screen-show-modal-a').addClass('active').append($oneListArr);
            //添加两种选择模块外层
            outHtml.append(htmlObj.screenModal.strFormat({
                'screenClass': 'screen-modal-a'
            }) + htmlObj.screenModal.strFormat({
                'screenClass': 'screen-modal-b'
            }));

            if(newObj.selCountry){
                outHtml.find('.screen-a[data-value="'+newObj.selCountry+'"]').addClass('active');
            }else {
                outHtml.find('.screen-a:first-child').addClass('active');
            }
            _this.append(outHtml);//生成对象内放入html
		
           //请求后台
           MYJ.ajax({
               url: 'queryCondition/index.json',
               data: {type: 1},
               loading: false,
               success: function (data) {
                   allCountryData = data.map;
                   if(data.queryCondition){
                       commoneData = data.queryCondition.condition;
                       if(commoneData){
                           var serchReg = new RegExp('\\[');
                           if(!serchReg.test(commoneData)){
                               commoneData = JSON.stringify([{groupName:'组合1',groupValue:commoneData}]);
                           }
                       }
                   }
                   var screenShowModalA = $('.screen-show-modal-a');
                   selGroupBuid(screenShowModalA,0);
               },
               error: function () {
                   //$('#loading').modal('hide');
				   // 测试数据
				   			var data = queryConditionData();
				   			allCountryData = data.map;
				                   if(data.queryCondition){
				                       commoneData = data.queryCondition.condition;
				                       if(commoneData){
				                           var serchReg = new RegExp('\\[');
				                           if(!serchReg.test(commoneData)){
				                               commoneData = JSON.stringify([{groupName:'组合1',groupValue:commoneData}]);
				                           }
				                       }
				                   }
				                   var screenShowModalA = $('.screen-show-modal-a');
				                   selGroupBuid(screenShowModalA,0);
               }
           });
            var selGroupBuid = function(outObj,type){
                if(commoneData){
                    var newCommoneDataObj = JSON.parse(commoneData),
                        $listArr = [];
                    $.each(newCommoneDataObj,function(i, j){
                        var oneData = j,
                            listStr,
                            groupName = oneData.groupName,
                            newGroupValue = oneData.groupValue;
                        if(type == 1){
                            listStr = $(htmlObj.listAGroup);
                            listStr.find('a').attr({
                                'code': newGroupValue,
                                'groupName': groupName,
                                'data-toggle': 'tooltip',
                                'data-placement': 'top',
                                'title': '组合国家：' + newGroupValue.replace(/,/g,', ')
                            }).html(groupName);
                        }else{
                            listStr = $(htmlObj.aSel);
                            listStr.attr({
                                'data-value': newGroupValue,
                                'groupName': groupName,
                                'uid': 'countryTag',
                                'data-toggle': 'tooltip',
                                'data-placement': 'top',
                                'title': '组合国家：' + newGroupValue.replace(/,/g,', ')
                            }).html(groupName);
                        }
                        $listArr.push(listStr);
                    });
                    $listArr.length && outObj.append($listArr);
                    $('.country-screen-out [data-toggle="tooltip"]').tooltip();
                }
            };
            //筛选框build
            var selCountryBuild = function(){
                var objA = _this.find('.screen-modal-a'),//筛选框a
                    objB = _this.find('.screen-modal-b');//筛选框b
                var allListOutA = $(htmlObj.listOut),//a里的全部
                    allListOutB = $(htmlObj.listOut),//b里的全部
                    hotListOutA = $(htmlObj.listOut),//a里的热门
                    hotListOutB = $(htmlObj.listOut),//b里的热门
                    euListOutA = $(htmlObj.listOut),//a里的欧盟
                    euListOutB = $(htmlObj.listOut);//b里的欧盟

                allListOutA.attr('cid',0);
                hotListOutA.attr('cid',8).addClass('active');//默认显示全部
                euListOutA.attr('cid', 9);
                allListOutB.attr('cid',0).append(htmlObj.listAll);//全部里添加全选checkbox
                hotListOutB.attr('cid',8).addClass('active')//默认显示全部
                    .append(htmlObj.listAll);//热门里添加全选checkbox
                euListOutB.attr('cid', 9).append(htmlObj.listAll);//欧盟里添加全选checkbox

                var hotData = newObj.hotCountry,
                    hotNum = newObj.hotShowNum,
                    listAStr = '',
                    listBStr = '';
                $.each(hotData,function(index){
                    if(index < hotNum){
                        var oneData = hotData[index];
                        listAStr += htmlObj.listA.strFormat(oneData);
                        listBStr += htmlObj.listB.strFormat(oneData);
                    }
                });
                listAStr && hotListOutA.append(listAStr);
                listBStr && hotListOutB.append(listBStr);
                selGroupBuid(hotListOutA,1);//在热门单选里生成保存的常用分组

                //欧盟筛选项数据生成
                var euData = newObj.euCountry,
                    euListAAllStr = '',
                    euListBAllStr = '';

                $.each(euData, function(index) {
                    var oneData = euData[index];

                    if (oneData.countryCh.length  > 6) {
                        euListAAllStr +=  htmlObj.listAUsTit.strFormat(oneData);
                        euListBAllStr += htmlObj.listBUsTit.strFormat(oneData);
                    } else {
                        euListAAllStr +=  htmlObj.listA.strFormat(oneData);
                        euListBAllStr += htmlObj.listB.strFormat(oneData);
                    }
                });
                euListAAllStr && euListOutA.append(euListAAllStr);
                euListBAllStr && euListOutB.append(euListBAllStr);

                var listOutAArr = [],
                    listOutBArr = [],
                    oneListAStr = '',
                    oneListBStr = '';
                $.each(allCountryData,function(dataname,datavalue){
                    var listOutA = $(htmlObj.listOut),
                        listOutB = $(htmlObj.listOut),
                        oneListAAllStr = '',
                        oneListBAllStr = '';
                    $.each(datavalue,function(index){
                        var oneData = datavalue[index];
                        if(oneData.countryCh.length > 6){
                            oneListAAllStr += htmlObj.listAUsTit.strFormat(oneData);
                            oneListBAllStr += htmlObj.listBUsTit.strFormat(oneData);
                        }else{
                            oneListAAllStr += htmlObj.listA.strFormat(oneData);
                            oneListBAllStr += htmlObj.listB.strFormat(oneData);
                        }
                        allCountryQueryObj[oneData.code] = {countryCh:oneData.countryCh,countryEn:oneData.countryEn};//生成查询对象
                    });
                    listOutA.attr('cid',dataname).append(oneListAAllStr);
                    listOutB.attr('cid',dataname).append(htmlObj.listAll + oneListBAllStr);
                    oneListAStr += oneListAAllStr;
                    oneListBStr += oneListBAllStr;
                    listOutAArr.push(listOutA);
                    listOutBArr.push(listOutB);
                });
                oneListAStr && allListOutA.append(oneListAStr);
                oneListBStr && allListOutB.append(oneListBStr);
                listOutAArr.length && objA.find('.content').append(listOutAArr);
                listOutBArr.length && objB.find('.content').append(listOutBArr);
                objA.find('.content').append([allListOutA, hotListOutA, euListOutA]);
                objB.find('.content').append([allListOutB, hotListOutB, euListOutB]);
                objB.append(htmlObj.selList + htmlObj.operationBtnGroup);
                $('.screen-operation-modal a').attr('showType', 1);
                $('.country-screen-out [data-toggle="tooltip"]').tooltip();
            };
            //更多，多选点击共用方法
            var screenOperation = function($obj){
                var showType = $obj.attr('showType'),
                    clickType = $obj.attr('clickType'),
                    modalName = $obj.attr('cid'),
                    objA = _this.find('.screen-modal-a'),
                    objB = _this.find('.screen-modal-b');
                if(showType == '0'){
                    selCountryBuild();//build页面
                }
                if(modalName == 'a' && clickType == 0){
                    $obj.attr('modalBShow', _this.find('.screen-show-modal-b').hasClass('active') ? 1 : 0);
                }
                _this.find('[cid="screen-show-modal"]').removeClass('active');
                _this.find('.screen-modal').removeClass('active');
                if(modalName == 'a'){
                    var nameSpan = $obj.find('span[cid="name"]'),
                        creatSpan = $obj.find('span[cid="creat"]'),
                        screenOperationB = $('.screen-operation-modal a[cid="b"]');
                    if(clickType == 0){
                        $obj.attr('clickType',1);
                        nameSpan.text('收起');
                        creatSpan.removeClass('downCreat').addClass('upCreat');
                        screenOperationB.removeClass('active');
                        objA.addClass('active');
                    }else{
                        $obj.attr('clickType',0);
                        nameSpan.text('更多');
                        creatSpan.removeClass('upCreat').addClass('downCreat');
                        screenOperationB.addClass('active');
                        objA.removeClass('active');
                        var modalBShow = $obj.attr('modalBShow');
                        if(modalBShow == 1){
                            _this.find('.screen-show-modal-b').addClass('active');
                        }else{
                            _this.find('.screen-show-modal-a').addClass('active');
                        }
                    }
                }else{
                    var screenOperationA = $('.screen-operation-modal a[cid="a"]');
                    screenOperationA.removeClass('active');
                    objB.addClass('active');
                    if(clickType == 0) {
                        $obj.attr('clickType', 1);
                    }
                }
            };
            //还原到初始筛选状态 单选
            var initializationFnA = function(){
                var outObj = $('.country-screen-out'),
                    $screenOperationA = outObj.find('.screen-operation-modal a');

                $screenOperationA.addClass('active').attr('clickType',0)
                    .filter('[cid="a"]').find('span[cid="name"]').html('更多');
                $screenOperationA.filter('[cid="a"]').find('span[cid="creat"]').removeClass('upCreat').addClass('downCreat');
                outObj.find('[cid="screen-show-modal"]').removeClass('active');
                outObj.find('.screen-show-modal-a').addClass('active');
                outObj.find('.screen-modal').removeClass('active');
                outObj.find('.list-o').removeClass('myj-hide');
                outObj.find('.search-group input[type="text"]').val('');
            };
            //还原到初始筛选状态 多选
            var initializationFnB = function(type,code,value){
                var outObj = $('.country-screen-out'),
                    screenListb = outObj.find('.screen-show-modal-b span[cid="screenList"]').html();
                if(type == 0 && screenListb == ''){
                    outObj.find('.screen-operation-modal a[cid="b"]').attr('clickType',0);
                    outObj.find('.screen-operation-modal a').removeClass('active').addClass('active');
                    outObj.find('.screen-show-modal-a').addClass('active');
                }else if(type == 1){
                    var screenShowB = outObj.find('.screen-show-modal-b');
                    screenShowB.find('span[cid="screenList"]').html(value);
                    screenShowB.addClass('active');
                    option.callBack(code);
                }else if(type == 2){
                    outObj.find('.screen-operation-modal a[cid="b"]').attr('clickType',0);
                    outObj.find('.screen-operation-modal a').removeClass('active').addClass('active');
                    outObj.find('.screen-show-modal-a').addClass('active');
                    outObj.find('.screen-show-modal-b').removeClass('active');
                    outObj.find('input[type="checkbox"]').prop('checked',false);
                    outObj.find('.selList').empty();
                }else{
                    outObj.find('.screen-show-modal-b').addClass('active');
                }
                outObj.find('.screen-modal').removeClass('active');
                var $screenModalB = outObj.find('.screen-modal-b');
                $screenModalB.find('.tit a').removeClass('active')
                    .filter('[cid="8"]').addClass('active');
                $screenModalB.find('.content .list').removeClass('active')
                    .filter('[cid="8"]').addClass('active');
                outObj.find('.list-o').removeClass('myj-hide');
                outObj.find('.search-group input[type="text"]').val('');
            };
            //单选共用选中事件
            var addActiveFn = function(code, groupname){
                var screenShowModalA = $('.screen-show-modal-a'),
                    $chooseA = screenShowModalA.find('a[data-value="'+code+'"]');

                screenShowModalA.find('a').removeClass('active');
                if (groupname !== undefined) {//修复分组内容一致被选中多个的情况
                    $chooseA.filter('[groupname="' + groupname + '"]').addClass('active');
                } else {
                    $chooseA.addClass('active');
                }
                option.callBack(code);
            };
            //删除组合用方法
            var removeGroupFn = function(code,groupName){
                var screenShowModalA = $('.screen-show-modal-a'),
                    countryTag = screenShowModalA.find('a[data-value="'+code+'"][groupName="'+groupName+'"]');
                if(countryTag.length){
                    if(countryTag.hasClass('active')){
                        screenShowModalA.find('a').removeClass('active');
                        screenShowModalA.find('a[data-value=""]').addClass('active');
                        option.callBack('');
                    }
                    countryTag.remove();
                }
                var newCommoneData = JSON.parse(commoneData),
                    indexNum;
                $.each(newCommoneData,function(i){
                    var oneData = newCommoneData[i];
                    if(oneData.groupName == groupName){
                        indexNum = i;
                        return false;
                    }
                });
                newCommoneData.splice(indexNum,1);
                commoneData = JSON.stringify(newCommoneData);
                addQueryCountry();
            };
            //注册‘更多’和‘多选点击事件’
            $(doc).off('click','.screen-operation-modal a');
            $(doc).on('click','.screen-operation-modal a',function(){
                var that = $(this);
                if(!allCountryData){
                    screenOperation(that);
                }else{
                    var countryState = $('.multipleChoiceModal').css('display');
                    if (countryState === 'block') {
                        $.fn.message({type:'error',msg:'请先确认好物流方式！'});
                        return false
                    }
                    screenOperation(that);
                }

            });
            //常用组合保存
            var addQueryCountry = function(){
                $.ajax({
                    type: "POST",
                    url: 'queryCondition/update.json',
                    data: {"condition": commoneData},
                    dataType: "json",
                    success: function (data) {
                    },
                    error: function () {
                        $('#loading').modal('hide');
                    }
                });
            };
            //注册选择洲按钮点击事件
            $(doc).off('click','.screen-modal .tit a');
            $(doc).on('click','.screen-modal .tit a',function(){
                var cid = $(this).attr('cid'),
                    modalOut = $(this).closest('.screen-modal'),
                    tit = $(this).closest('.tit');
                modalOut.find('.list').removeClass('active');
                tit.find('a').removeClass('active');
                $(this).addClass('active');
                modalOut.find('.list[cid="'+cid+'"]').addClass('active');
            });
            //多选确定后方法
            var selEnterFn = function(){
                var selListObj = $('.country-screen-out .selListOut input[type="checkbox"]'),
                    newCommonCountryStr = '',
                    newCommonCountryNameStr = '';
                selListObj.each(function(){
                    var iptVal = $(this).attr('code'),
                        iptNameVal = $(this).attr('chName');
                    if (iptVal && iptNameVal) {
                        if(newCommonCountryStr){
                            newCommonCountryStr += ','+iptVal;
                            newCommonCountryNameStr += '、'+iptNameVal;
                        }else{
                            newCommonCountryStr += iptVal;
                            newCommonCountryNameStr += iptNameVal;
                        }
                    }
                });
                if(newCommonCountryStr){
                    //initializationFnB(1,newCommonCountryStr);

                    var commonCountryObj;
                    commoneData ? commonCountryObj = JSON.parse(commoneData) : commonCountryObj = [];
                    if(commonCountryObj.length < 10){
                        var modalObj = $(htmlObj.modalHtml);
                        modalObj.find('#thisGroupData').attr('groupCode',newCommonCountryStr).attr('groupName',newCommonCountryNameStr);
                        modalObj.attr('id','countryGroupModal');
                        $('body').append(modalObj);
                        $('#countryGroupModal').modal('show');
                        $('#countryGroupModal[cid="countryGroupModal"] input[type="text"]').focus();
                    }else{
                        initializationFnB(1,newCommonCountryStr,newCommonCountryNameStr);
                    }
                }
            };
            //注册多选button事件
            $(doc).off('click','.screen-modal .operation-btn-group button');
            $(doc).on('click','.screen-modal .operation-btn-group button',function(){
                var type = $(this).attr('cid');
                if(type === 'enter'){
                    selEnterFn();
                }else{
                    initializationFnB(0);
                }
            });
            //注册筛选里单选事件
            $(doc).off('click', '.screen-modal-a .list-o, .screen-modal-a .list-group a')
                .on('click','.screen-modal-a .list-o, .screen-modal-a .list-group a',function(){
                var thisCode = $(this).attr('code'),
                    type = $(this).attr('clickType'),
                    showName;
                if(type === 'one'){
                    showName = $(this).attr('chName');
                }else if(type === 'group'){
                    showName = $(this).attr('groupName');
                }
                var screenShowModalA = $('.screen-show-modal-a');
                if(screenShowModalA.find('a[data-value="'+thisCode+'"]').length === 0){
                    if(screenShowModalA.find('a[data-type="new"]').length === 0){
                        var newScreenA = $(htmlObj.aSel);
                        newScreenA.attr('data-value',thisCode).attr('data-type','new').attr('uid','countryTag').html(showName);
                        screenShowModalA.append(newScreenA);
                    }else{
                        screenShowModalA.find('a[data-type="new"]').attr('data-value',thisCode).html(showName);
                    }
                }
                $(this).closest('.screen-modal').find('.search-group span[cid="clearAllSearchVal"]').removeClass('active');
                addActiveFn(thisCode, (type === 'group' ? showName : undefined));//走共用的选中方法
                initializationFnA();//还原到初始筛选状态
            });
            //注册初始单选事件
            $(doc).off('click','.screen-show-modal-a .screen-a');
            $(doc).on('click','.screen-show-modal-a .screen-a',function(){
                var thisCode = $(this).attr('data-value');
                addActiveFn(thisCode, $(this).attr('groupname'));
            });
            //注册筛选里组合删除事件
            $(doc).off('click','.screen-modal span[cid="groupRemove"]');
            $(doc).on('click','.screen-modal span[cid="groupRemove"]',function(){
                var listGroup = $(this).closest('.list-group'),
                    aobj = listGroup.find('a'),
                    thisCode = aobj.attr('code'),
                    groupName = aobj.attr('groupName');
                myjAlert.confirm({
                    'content':'确定删除组合？',
                    'callback':function(v){
                        if(v){
                            removeGroupFn(thisCode,groupName);
                            listGroup.remove();
                        }
                    }});
            });
            //多选里 box选中、取消选中处理
            var boxCheckedHandle = function(obj,listOut,selListOut,type){
                var boxVal = $(obj).val(),
                    code = $(obj).attr('code'),
                    listNum = $(obj).closest('.list').attr('cid');
                if(boxVal === 'all'){
                    if(listNum == 0){
                        $(listOut).find('input[type="checkbox"]').prop('checked', !(type == 0))
                    }else{
                        var listObj = $(obj).closest('.list');
                        listObj.find('input[type="checkbox"]').each(function(){
                            var thisCode = $(this).attr('code'),
                                thisVal = $(this).val();
                            if(thisVal !== 'all'){
                                $(listOut).find('input[type="checkbox"][code="'+thisCode+'"]').prop('checked', !(type == 0));
                            }
                        });
                    }
                }else{
                    $(listOut).find('input[type="checkbox"][code="'+code+'"]').prop('checked', !(type == 0));
                }
                isAllCheckedHandle();//是否选中全选处理
                buildBoxList();//生成selList
            };
            //生成selList
            var buildBoxList = function(){
                var allBoxObj = _this.find('.screen-modal-b .content .list[cid="0"] input[type="checkbox"]:checked'),
                    listOutObj = _this.find('.screen-modal-b .selList'),
                    newStr = '<label class="list-o"><input type="checkbox" name="selList" checked /></label>',
                    newStrArr = [];
                listOutObj.empty();
                allBoxObj.each(function(){
                    if($(this).val() !== 'all'){
                        var thisCode = $(this).attr('code'),
                            chName = $(this).attr('chName'),
                            enName = $(this).attr('enName'),
                            newStrObj = $(newStr);
                        newStrObj.append(chName).find('input').attr({
                            'code': thisCode,
                            'chName': chName,
                            'enName': enName
                        }).val(enName);
                        newStrArr.push(newStrObj);
                    }
                });
                newStrArr.length && listOutObj.append(newStrArr);
            };
            //多选里 是否选中全选处理
            var isAllCheckedHandle = function(){
                _this.find('.screen-modal-b .content .list').each(function(){
                    var allLength = $(this).find('input:checkbox[value!="all"]:not(":checked")').length;

                    $(this).find('input[type="checkbox"][value="all"]').prop('checked', !allLength);
                });
            };
            //多选box注册事件
            $(doc).off('click','.screen-modal-b .content input[type="checkbox"]');
            $(doc).on('click','.screen-modal-b .content input[type="checkbox"]',function(){
                var listOut = _this.find('.screen-modal-b .content'),
                    selListOut = _this.find('.screen-modal .selList'),
                    type = $(this).is(':checked') ? 1 : 0;

                boxCheckedHandle($(this), listOut, selListOut, type);
            });
            //多选 sellist box 注册事件
            $(doc).off('click','.screen-modal-b .selList input[type="checkbox"]');
            $(doc).on('click','.screen-modal-b .selList input[type="checkbox"]',function(){
                if(!$(this).is(':checked')){
                    var thisCode = $(this).attr('code');
                    _this.find('.screen-modal-b .content input[type="checkbox"][code="'+thisCode+'"]').prop('checked',false);
                    isAllCheckedHandle();//是否选中全选处理
                    buildBoxList();//生成selList
                }
            });
            //弹层确定事件
            $(doc).off('click','#countryGroupModal button[cid="enter"]');
            $(doc).on('click','#countryGroupModal button[cid="enter"]',function(){
                var modalObj = $(this).closest('.modal'),
                    groupValue = $(modalObj).find('#thisGroupData').attr('groupCode'),
                    groupName = $.trim(modalObj.find('input[type="text"]').val()),
                    commonCountryObj,
                    nameArr = [];

                commonCountryObj = (commoneData ? JSON.parse(commoneData) : []);
                $.each(commonCountryObj,function(i){
                    nameArr.push(commonCountryObj[i].groupName);
                });
                if(groupName == ''){
                    $.fn.message({type: "error", msg: "请输入组合名称"});
                    return;
                }
                var nameReg = new RegExp(/^[a-zA-Z0-9\u4E00-\u9FA5]{1,20}$/);
                if(!nameReg.test(groupName)){
                    $.fn.message({type: "error", msg: "组合名称只支持中文、英文及数字"});
                    return;
                }
                if($.inArray(groupName,nameArr) != -1){
                    $.fn.message({type: "error", msg: "组合名称重复，请使用其它名称"});
                    return;
                }
                //var newCommoneData = JSON.parse(commoneData);
                commonCountryObj.push({groupName:groupName,groupValue:groupValue});
                commoneData = JSON.stringify(commonCountryObj);
                var listGroupHtml = $(htmlObj.listAGroup),
                    newGroupValue = groupValue.replace(/,/g,', '),
                    $countryScreenOut = $('.country-screen-out');
                listGroupHtml.find('a').attr({
                    'code': groupValue,
                    'groupName': groupName,
                    'title': '组合国家：' + newGroupValue,
                    'data-toggle': 'tooltip',
                    'data-placement': 'top'
                }).html(groupName);
                $countryScreenOut.find('.screen-modal-a .content .list[cid="8"]').append(listGroupHtml);
                addQueryCountry();
                var aSel = $(htmlObj.aSel);
                aSel.attr({
                    'data-value': groupValue,
                    'groupName': groupName,
                    'uid': 'countryTag',
                    'title': '组合国家：'+newGroupValue,
                    'data-toggle': 'tooltip',
                    'data-placement': 'top'
                }).html(groupName).addClass('active');
                $countryScreenOut.find('.screen-show-modal-a a').removeClass('active');
                $countryScreenOut.find('.screen-show-modal-a').append(aSel);
                $countryScreenOut.find('[data-toggle="tooltip"]').tooltip();
                $countryScreenOut.find('input[type="checkbox"]').prop('checked',false);
                $countryScreenOut.find('.selList').empty();
                initializationFnB(1,groupValue);
                initializationFnA();
                modalObj.modal('hide');
                modalObj.remove();
            });
            //弹层取消事件
            $(doc).off('click','#countryGroupModal button[cid="cancel"]');
            $(doc).on('click','#countryGroupModal button[cid="cancel"]',function(){
                var modalObj = $(this).closest('.modal'),
                    $thisGroupData = modalObj.find('#thisGroupData'),
                    groupCode = $thisGroupData.attr('groupCode'),
                    groupName = $thisGroupData.attr('groupName');
                initializationFnB(1,groupCode,groupName);
                modalObj.modal('hide');
                modalObj.remove();
            });
            //弹层ipt回车事件
            $(doc).off('keydown','#countryGroupModal[cid="countryGroupModal"] input[type="text"]');
            $(doc).on('keydown','#countryGroupModal[cid="countryGroupModal"] input[type="text"]',function(event){
                event = event || window.event;
                if(event.which == '13'){
                    $(this).closest('.modal').find('button[cid="enter"]').click();
                }
            });
            //清除多选注册事件
            $(doc).off('click','.country-screen-out .remove-screen');
            $(doc).on('click','.country-screen-out .remove-screen',function(){
                var $countryScreenOut = $('.country-screen-out');
                $countryScreenOut.find('.screen-show-modal-a a').removeClass('active');
                $countryScreenOut.find('.screen-show-modal-a a:first-child').addClass('active');
                $countryScreenOut.find('.screen-show-modal-b span[cid="screenList"]').empty();
                initializationFnB(2);
                option.callBack('');
            });
            //搜索清除
            $(doc).off('click','.country-screen-out .search-group span[cid="clearAllSearchVal"]');
            $(doc).on('click','.country-screen-out .search-group span[cid="clearAllSearchVal"]',function(){
                var $that = $(this);
                $that.removeClass('active').closest('.screen-modal .content').find('.list-o, .list-group').removeClass('myj-hide');
                $that.closest('.search-group').find('input[type="text"]').val('').focus();
            });
            //ipt搜索
            $(doc).off('keyup','.country-screen-out .search-group input[type="text"]');
            $(doc).on('keyup','.country-screen-out .search-group input[type="text"]',function(){
                var $that = $(this),
                    iptVal = $that.val(),
                    outObj = $that.closest('.screen-modal').find('.content'),
                    list = outObj.find('.list-o'),
                    allTit = $that.closest('.tit').find('a[cid="0"]');
                if(!allTit.hasClass('active')){
                    $that.closest('.tit').find('a').removeClass('active');
                    allTit.addClass('active');
                    outObj.find('.list').removeClass('active')
                        .filter('[cid="0"]').addClass('active');
                }
                var clearBtn = $that.closest('.search-group').find('span[cid="clearAllSearchVal"]');
                if(iptVal){
                    clearBtn.addClass('active');
                    var serchReg1 = new RegExp(iptVal),
                        serchReg2 = new RegExp(iptVal.toLowerCase());
                    outObj.find('.list-group').addClass('myj-hide');
                    outObj.find('.list-o').addClass('myj-hide');
                    list.each(function(){
                        var $j = $(this),
                            tagname = $j.get(0).tagName,
                            code,enName,chName;
                        if(tagname == 'A'){
                            code = $j.attr('code');
                            enName = $j.attr('enName');
                            chName = $j.attr('chName');
                        }else{
                            code = $j.find('input').attr('code');
                            enName = $j.find('input').attr('enName');
                            chName = $j.find('input').attr('chName');
                        }
                        var type = 0;
                        if(code && enName && chName){
                            if(serchReg1.test(chName)){
                                type = 1;
                            }else if(serchReg2.test(code.toLowerCase())){
                                type = 1;
                            }else if(serchReg2.test(enName.toLowerCase())){
                                type = 1;
                            }
                        }
                        if(type == 1){
                            $j.removeClass('myj-hide');
                        }
                    });
                }else{
                    clearBtn.removeClass('active');
                    outObj.find('.list-group').removeClass('myj-hide');
                    outObj.find('.list-o').removeClass('myj-hide');
                }
            });
		},
        defaultsHtml:{
            outHtml:'<div class="country-screen-out"></div>',
            screenOptionModal:'<div class="screen-operation-modal">' +
                '<div class="operation-btn-out"><a id="countryCheck" href="javascript:" class="active" cid="b" showType="0" clickType="0">多选 <span style="font-size:16px;color:#666;">+</span></a></div>' +
                '<div class="operation-btn-out"><a href="javascript:" class="active" cid="a" showType="0" clickType="0" id="moreBtn"><span cid="name">更多</span> <span class="downCreat" cid="creat"></span></a></div>' +
                '</div>',
    aSel:'<a class="screen-a" href="javascript:"></a>',
            screenShowModal:'<div class="screen-show-modal-a countryAreaScreenBox" cid="screen-show-modal"></div><div class="screen-show-modal-b" cid="screen-show-modal"><span cid="screenList"></span><a class="remove-screen" href="javascript:">清除多选 X</a></div>',
            screenModal:'<div class="screen-modal #{screenClass}"><div class="tit">' +
            '<a href="javascript:" class="classify-a active" cid="8">热门</a>' +
            '<a href="javascript:" class="classify-a" cid="0">全部</a>' +
            '<a href="javascript:" class="classify-a" cid="1">欧洲</a>' +
            '<a href="javascript:" class="classify-a" cid="9">欧盟</a>' +
            '<a href="javascript:" class="classify-a" cid="2">北美</a>' +
            '<a href="javascript:" class="classify-a" cid="3">南美</a>' +
            '<a href="javascript:" class="classify-a" cid="4">亚洲</a>' +
            '<a href="javascript:" class="classify-a" cid="5">大洋洲</a>' +
            '<a href="javascript:" class="classify-a" cid="6">非洲</a>' +
            '<a href="javascript:" class="classify-a" cid="7">其它</a>' +
            '<div class="search-group"><input type="text"/><span class="glyphicon glyphicon-search"></span><span cid="clearAllSearchVal">清除</span></div>' +
            '</div><div class="content p10"></div>' +
            '</div>',
            listA:'<a class="list-o" clickType="one" href="javascript:" code="#{code}" chName="#{countryCh}" enName="#{countryEn}">#{countryCh}</a>',
            listAUsTit:'<a class="list-o usTooltip" data-toggle="tooltip" data-placement="top" title="#{countryCh}" clickType="one" href="javascript:" code="#{code}" chName="#{countryCh}" enName="#{countryEn}">#{countryCh}</a>',
            listAGroup:'<div class="list-group" ><a clickType="group" href="javascript:" code=""></a><span class="glyphicon glyphicon-remove-circle" cid="groupRemove"></span></div>',
            listB:'<label class="list-o"><input type="checkbox" name="Multiselect"  code="#{code}" chName="#{countryCh}" enName="#{countryEn}" value="#{countryEn}"/>#{countryCh}</label>',
            listBUsTit:'<label class="list-o usTooltip" data-toggle="tooltip" data-placement="top" title="#{countryCh}"><input type="checkbox" name="Multiselect"  code="#{code}" chName="#{countryCh}" enName="#{countryEn}" value="#{countryEn}"/>#{countryCh}</label>',
            listAll:'<label class="list-o"><input type="checkbox" name="Multiselect" value="all"/>全部</label>',
            listOut:'<div class="list"></div>',
            selList:'<div class="selListOut"><table><tr><td><span class="title">已选国家/地区：</span></td><td><div class="selList"></div></td></tr></table></div>',
            operationBtnGroup:'<div class="operation-btn-group"><button class="button btn-determine" cid="enter" type="button">确认</button><button class="button btn-gray m-left10" cid="cancel" type="button">取消</button></div>',
            modalHtml:'<div class="modal" id="countryGroupModal" cid="countryGroupModal" tabindex="-1" role="dialog" aria-hidden="false" data-backdrop="static" data-keyboard="false">' +
                '<div class="modal-dialog">' +
                '<div class="modal-content">' +
                '<div class="modal-header">' +
                '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
                '<h4 class="modal-title">保存组合</h4>' +
                '</div>' +
                '<div class="modal-body"><input type="hidden" id="thisGroupData">' +
                '<p>保存所选国家为常用组合，方便以后查询</p>'+
                '<p><strong>组合名称：</strong><input type="text" maxlength="6" placeholder="请填写名称"/></p>'+
                '<p><span class="explainTit">说明！</span><span class="explainContent">请使用6个以内的汉字或英文字符。</span></p>'+
                '</div>' +
                '<div class="modal-footer">' +
                '<button type="button" class="button btn-determine" cid="enter">保存</button>' +
                '<button type="button" class="button btn-gray m-left10" cid="cancel">先不保存</button>' +
                '</div></div></div></div>'
        },
        defaults : {
            commonCountry:[
                {name:'全部',code:''},
                {name:'美国',code:'US'},
                {name:'加拿大',code:'CA'},
                {name:'英国',code:'GB'},
                {name:'法国',code:'FR'},
                {name:'德国',code:'DE'},
                {name:'俄罗斯',code:'RU'},
                {name:'巴西',code:'BR'},
                {name:'澳大利亚',code:'AU'},
                {name:'日本',code:'JP'},
                {name:'西班牙',code:'ES'},
                {name:'意大利',code:'IT'},
                {name:'荷兰',code:'NL'},
                {name:'瑞典',countryEn:'SWEDEN',code:'SE'},
                {name:'以色列',countryEn:'ISRAEL',code:'IL'},
                {name:'瑞士',countryEn:'SWITZERLAND',code:'CH'},
                {name:'乌克兰',countryEn:'UKRAINE',code:'UA'},
                {name:'波多黎各',countryEn:'PUERTO RICO',code:'PR'},
                {name:'比利时',countryEn:'BELGIUM',code:'BE'},
                {name:'捷克',countryEn:'CZECH REPUBLIC',code:'CZ'},
                {name:'挪威',countryEn:'NORWAY',code:'NO'},
                {name:'波兰',countryEn:'POLAND',code:'PL'},
                {name:'智利',countryEn:'CHILE',code:'CL'}/*,
                {name:'丹麦',countryEn:'DENMARK',code:'DK'},
                {name:'芬兰',countryEn:'FINLAND',code:'FI'},
                {name:'土耳其',countryEn:'TURKEY',code:'TR'},
                {name:'墨西哥',countryEn:'MEXICO',code:'MX'},
                {name:'白俄罗斯',countryEn:'BELARUS',code:'BY'}*/
            ],
            hotCountry:[
                {countryCh:'美国',countryEn:'UNITED STATES',code:'US'},
                {countryCh:'俄罗斯',countryEn:'RUSSIA',code:'RU'},
                {countryCh:'法国',countryEn:'FRANCE',code:'FR'},
                {countryCh:'英国',countryEn:'UNITED KINGDOM',code:'GB'},
                {countryCh:'德国',countryEn:'GERMANY',code:'DE'},
                {countryCh:'加拿大',countryEn:'CANADA',code:'CA'},
                {countryCh:'西班牙',countryEn:'SPAIN',code:'ES'},
                {countryCh:'巴西',countryEn:'BRAZIL',code:'BR'},
                {countryCh:'意大利',countryEn:'ITALY',code:'IT'},
                {countryCh:'澳大利亚',countryEn:'AUSTRALIA',code:'AU'},
                {countryCh:'荷兰',countryEn:'NETHERLANDS',code:'NL'},
                {countryCh:'瑞典',countryEn:'SWEDEN',code:'SE'},
                {countryCh:'以色列',countryEn:'ISRAEL',code:'IL'},
                {countryCh:'瑞士',countryEn:'SWITZERLAND',code:'CH'},
                {countryCh:'乌克兰',countryEn:'UKRAINE',code:'UA'},
                {countryCh:'波多黎各',countryEn:'PUERTO RICO',code:'PR'},
                {countryCh:'比利时',countryEn:'BELGIUM',code:'BE'},
                {countryCh:'捷克',countryEn:'CZECH REPUBLIC',code:'CZ'},
                {countryCh:'挪威',countryEn:'NORWAY',code:'NO'},
                {countryCh:'波兰',countryEn:'POLAND',code:'PL'},
                {countryCh:'智利',countryEn:'CHILE',code:'CL'},
                {countryCh:'丹麦',countryEn:'DENMARK',code:'DK'},
                {countryCh:'芬兰',countryEn:'FINLAND',code:'FI'},
                {countryCh:'土耳其',countryEn:'TURKEY',code:'TR'},
                {countryCh:'墨西哥',countryEn:'MEXICO',code:'MX'},
                {countryCh:'白俄罗斯',countryEn:'BELARUS',code:'BY'},
                {countryCh:'日本',countryEn:'JAPAN',code:'JP'},
                {countryCh:'菲律宾',countryEn:'PHILIPPINES',code:'PH'}
            ],
            hotShowNum:21,
            euCountry:[
                {countryCh:'奥地利',countryEn:'AUSTRIA',code:'AT'},
                {countryCh:'比利时',countryEn:'BELGIUM',code:'BE'},
                {countryCh: '保加利亚',countryEn:'BULGARIA',code:'BG'},
                {countryCh: '塞浦路斯',countryEn:'CYPRUS',code:'CY'},
                {countryCh: '克罗地亚',countryEn:'CROATIA',code:'HR'},
                {countryCh: '捷克共和国',countryEn:'CZECH REPUBLIC',code:'CZ'},
                {countryCh:'丹麦',countryEn:'DENMARK',code:'DK'},
                {countryCh:'爱沙尼亚',countryEn:'ESTONIA',code:'EE'},
                {countryCh:'芬兰',countryEn:'FINLAND',code:'FI'},
                {countryCh:'法国',countryEn:'FRANCE',code:'FR'},
                {countryCh:'德国',countryEn:'GERMANY',code:'DE'},
                {countryCh:'希腊',countryEn:'GREECE',code:'GR'},
                {countryCh:'匈牙利',countryEn:'HUNGARY',code:'HU'},
                {countryCh:'爱尔兰',countryEn:'IRELAND',code:'IE'},
                {countryCh:'意大利',countryEn:'ITALY',code:'IT'},
                {countryCh:'拉脱维亚',countryEn:'LATVIA',code:'LV'},
                {countryCh:'立陶宛',countryEn:'LITHUANIA',code:'LT'},
                {countryCh:'卢森堡',countryEn:'LUXEMBOURG',code:'LU'},
                {countryCh:'马耳他',countryEn:'MALTA',code:'MT'},
                {countryCh:'荷兰',countryEn:'NETHERLANDS',code:'NL'},
                {countryCh:'波兰',countryEn:'POLAND',code:'PL'},
                {countryCh:'葡萄牙',countryEn:'PORTUGAL',code:'PT'},
                {countryCh:'罗马尼亚',countryEn:'ROMANIA',code:'RO'},
                {countryCh:'斯洛伐克共和国',countryEn:'SLOVAKIA REPUBLIC',code:'SK'},
                {countryCh:'斯洛文尼亚',countryEn:'SLOVENIA',code:'SI'},
                {countryCh:'西班牙',countryEn:'SPAIN',code:'ES'},
                {countryCh:'瑞典',countryEn:'SWEDEN',code:'SE'}
            ],
            callBack:function(code){
            },
            selCountry:null     //默认选中项
        }
	})
})(jQuery,window,document);